//
//  AddCardVerifyViewController.m
//  HuLaQuan
//
//  Created by hok on 1/20/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "AddCardVerifyViewController.h"


//Jumpt Back To
#import "BankCardListViewController.h"
#import "QRCodeViewController.h"
#import "GenericConvenientPayHomeController.h"


//Back To


@interface AddCardVerifyViewController ()

@end

@implementation AddCardVerifyViewController
//- (IBAction)tapNextButton:(id)sender {
//    NSLog(@"haha");
//    if ([self.navigationController.viewControllers[1] isKindOfClass:[BankCardListViewController class]]) {
//        [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
//    }
//    
//}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    self.smsVerifyEngine.smsType = SMSAddCard;
    self.userInfo[@"userName"] = self.addCardCheckInfoEngine.cardInfo[@"phone"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addCardFinish:) name:@"AddCardFinish" object:nil];
    
    
}
- (void)verifySMSFinish:(id)message
{
    [YuNetworkStatusUtilities showProgressMessage:NETWORK_LOADING];
    [self.addCardCheckInfoEngine addCard:message[@"tokenValue"]];
    
//    if ([self.navigationController.viewControllers[1] isKindOfClass:[BankCardListViewController class]]) {
//                [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
//    }
}
- (void)addCardFinish:(NSNotification*) notification
{
    
    [YuNetworkStatusUtilities dismissHUD];
    
    NSDictionary *messageDic = [notification object];//获取到传递的对象
    if ([messageDic[@"isSuccess"] boolValue])
    {
        if ([self.navigationController.viewControllers[1] isKindOfClass:[BankCardListViewController class]])
        {
            [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
        }
        else if ([self.navigationController.viewControllers[1] isKindOfClass:[QRCodeViewController class]])
        {
            QRCodeViewController *controller = (QRCodeViewController *)self.navigationController.viewControllers[1];
            controller.isFromAddCardFinish = YES;
            [self.navigationController popToViewController:controller animated:YES];
        }else if([self.navigationController.viewControllers[1] isKindOfClass:[GenericConvenientPayHomeController class]])
        {
            GenericConvenientPayHomeController *controller = (GenericConvenientPayHomeController *)self.navigationController.viewControllers[1];
            controller.isFromAddCardFinish = YES;
            [self.navigationController popToViewController:controller animated:YES];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }

    }else{
        [self showNetWorkError:messageDic[@"message"]];
        
    }
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AddCardFinish" object:nil];
    
    
}

#pragma mark - Navigation Back
- (void)goBackAction
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    UIAlertController *x = [UIAlertController alertWithTitle:@"提示" andMessage:@"是否放弃绑定银行卡操作" andBlock:^(UIAlertAction *action ) {
        if ([action.title isEqualToString:@"好的"]) {
            if ([self.navigationController.viewControllers[1] isKindOfClass:[BankCardListViewController class]])
            {
                [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
            }
            else if ([self.navigationController.viewControllers[1] isKindOfClass:[QRCodeViewController class]])
            {
                [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
            }else if([self.navigationController.viewControllers[1] isKindOfClass:[GenericConvenientPayHomeController class]])
            {
                [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
            }else{
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
        
    }  type:@"confirmAndCancel"];
    [self presentViewController:x animated:YES completion:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
